﻿using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using O1.Kernel.Primitives;

namespace Tests.O1.Kernel.Primitives
{
    [TestClass]
    public class Int16PropertyTests
    {
        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyWritesDataIntoPackedSizeBufferAsExpected()
        {
            var subject = new Int16Property(6, 122);
            var buffer = new byte[Int16Property.Size];
            subject.Write(buffer, 0);
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyWritesDataValuesAsExpected()
        {
            var subject = new Int16Property(99, 12200);
            var buffer = new byte[Int16Property.Size];
            subject.Write(buffer, 0);

            var actualKey = BitConverter.ToInt32(buffer, 0);
            var actualValue = BitConverter.ToInt16(buffer, 4);

            actualValue.Should().Be(12200);
            actualKey.Should().Be(99);
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyReadsAsExpected()
        {
            var buffer = new byte[Int16Property.Size];
            var data = BitConverter.GetBytes((short)6780);
            var key = BitConverter.GetBytes(77);

            Array.Copy(key, 0, buffer, 0, 4);
            Array.Copy(data, 0, buffer, 4, 2);

            var subject = Int16Property.Read(buffer, 0);
            subject.Key.Should().Be(77);
            subject.Value.Should().Be(6780);
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyWritesThenReadsAsExpected()
        {
            var expected = new Int16Property(99, 12200);
            var buffer = new byte[Int16Property.Size];
            expected.Write(buffer, 0);

            var actual = Int16Property.Read(buffer, 0);
            actual.Key.Should().Be(expected.Key);
            actual.Value.Should().Be(expected.Value);
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyEqualityIsKeyValueEquality()
        {
            var subject1 = new Int16Property(99, 12200);
            var subject2 = new Int16Property(99, 12200);
            (subject1 == subject2).Should().BeTrue();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyInequalityIsKeyInequality()
        {
            var subject1 = new Int16Property(99, 12200);
            var subject2 = new Int16Property(98, 12200);
            (subject1 != subject2).Should().BeTrue();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyInequalityIsValueInequality()
        {
            var subject1 = new Int16Property(98, 4500);
            var subject2 = new Int16Property(98, 12200);
            (subject1 != subject2).Should().BeTrue();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyEqualsIsValueEquality()
        {
            var subject1 = new Int16Property(99, 12200);
            var subject2 = new Int16Property(99, 12200);
            subject1.Equals(subject2).Should().BeTrue();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyEqualsIsKeyInequality()
        {
            var subject1 = new Int16Property(99, 12200);
            var subject2 = new Int16Property(98, 12200);
            subject1.Equals(subject2).Should().BeFalse();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyEqualsIsValueInequality()
        {
            var subject1 = new Int16Property(98, 1200);
            var subject2 = new Int16Property(98, 12200);
            subject1.Equals(subject2).Should().BeFalse();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyIsNotEqualToNull()
        {
            object other = null;
            new Int16Property(98, 1200).Equals(other).Should().BeFalse();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyGetHashCodeIsConsistent()
        {
            var subject = new Int16Property(98, 1500);
            subject.GetHashCode().Should().Be(subject.GetHashCode());   
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyGetHashCodeIsDifferentForDifferentKey()
        {
            var subject1 = new Int16Property(98, 12500);
            var subject2 = new Int16Property(101, 12500);
            subject1.GetHashCode().Should().NotBe(subject2.GetHashCode());
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyGetHashCodeIsDifferentForDifferentValue()
        {
            var subject1 = new Int16Property(101, 12500);
            var subject2 = new Int16Property(101, 1200);
            subject1.GetHashCode().Should().NotBe(subject2.GetHashCode());
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyGetHashCodeIsDifferentForDifferentKeyAndValue()
        {
            var subject1 = new Int16Property(88, 14500);
            var subject2 = new Int16Property(101, 1200);
            subject1.GetHashCode().Should().NotBe(subject2.GetHashCode());
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyObjectEqualsIsEquality()
        {
            var subject1 = new Int16Property(98, 12400);
            object subject2 = new Int16Property(98, 12400);
            subject1.Equals(subject2).Should().BeTrue();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyObjectEqualsIsInequalityForDifferentKey()
        {
            var subject1 = new Int16Property(99, 12400);
            object subject2 = new Int16Property(98, 12400);
            subject1.Equals(subject2).Should().BeFalse();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyObjectEqualsIsInequalityForDifferentValue()
        {
            var subject1 = new Int16Property(99, 12500);
            object subject2 = new Int16Property(99, 500);
            subject1.Equals(subject2).Should().BeFalse();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyObjectEqualsIsInequalityForDifferentKeyAndValue()
        {
            var subject1 = new Int16Property(8769, 14500);
            object subject2 = new Int16Property(99, 500);
            subject1.Equals(subject2).Should().BeFalse();
        }

        [TestMethod, TestCategory("Unit Test")]
        public void Int16PropertyObjectEqualsIsInequalityForDifferentType()
        {
            var subject1 = new Int16Property(99, 14500);
            object subject2 = new object();
            subject1.Equals(subject2).Should().BeFalse();
        }
    }
}
